# set working directory
setwd('~/replication_files')

# function to load and install packages
load_packages<-function(packages){
  sapply(packages,function(x){
    f<-require(x,character.only=T)
    if(!f) install.packages(x); f<-require(x,character.only=T)
    return(f)},USE.NAMES=T)
}

# load packages
load_packages(c('data.table','ggplot2','gtools','gridExtra','diagis'))

# function to extract legend from ggplot object
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

# read in data
(FinDT<-readRDS('FigureA1.rds'))

# melt data.table
(binDT<-melt(FinDT,id.vars='FB_tercile',measure.vars=c('provax_bin_terc_pct','antivax_bin_terc_pct')))
# get lower and upper values
(binDTlo<-melt(FinDT,id.vars='FB_tercile',measure.vars=c('provax_bin_terc_pct_lo','antivax_bin_terc_pct_lo'),value.name='lo'))
(binDThi<-melt(FinDT,id.vars='FB_tercile',measure.vars=c('provax_bin_terc_pct_hi','antivax_bin_terc_pct_hi'),value.name='hi'))

# add lower and upper values
set(binDT,NULL,'lo',binDTlo[['lo']])
set(binDT,NULL,'hi',binDThi[['hi']])
# create factor of FB usage
set(binDT,NULL,'FB_tercile_fac',factor(binDT[['FB_tercile']],levels=1:3,labels=c('low','medium','high')))
# create factor of type of webpage
set(binDT,NULL,'vax_type_fac',factor(rep(1:2,each=3),levels=1:2,labels=c('Not vaccine-skeptical','Vaccine-skeptical')))

# do the same as the above except using the count variable
(countDT<-melt(FinDT,id.vars='FB_tercile',measure.vars=c('provax_count_terc','antivax_count_terc')))
(countDTlo<-melt(FinDT,id.vars='FB_tercile',measure.vars=c('provax_count_terc_lo','antivax_count_terc_lo'),value.name='lo'))
(countDThi<-melt(FinDT,id.vars='FB_tercile',measure.vars=c('provax_count_terc_hi','antivax_count_terc_hi'),value.name='hi'))

set(countDT,NULL,'lo',countDTlo[['lo']])
set(countDT,NULL,'hi',countDThi[['hi']])
set(countDT,NULL,'FB_tercile_fac',factor(countDT[['FB_tercile']],levels=1:3,labels=c('low','medium','high')))
set(countDT,NULL,'vax_type_fac',factor(rep(1:2,each=3),levels=1:2,labels=c('Not vaccine-skeptical','Vaccine-skeptical')))

# create legend
p1.leg<-
  ggplot(data=binDT,mapping=aes(x=FB_tercile_fac,y=value,group=vax_type_fac))+
  geom_col(mapping=aes(color=vax_type_fac,fill=vax_type_fac),position='dodge')+
  geom_linerange(aes(ymin=lo, ymax=hi),position=position_dodge(width=.9),color='gray80',size=.6)+
  scale_color_manual(values=c('gray60','black'))+
  scale_fill_manual(values=c('gray60','black'))+
  guides(color=guide_legend(title='Webpage type'),fill=guide_legend(title='Webpage type'))+
  theme_bw()+theme(axis.ticks=element_blank(),panel.border=element_blank(),panel.grid.major.x=element_blank(),
                   panel.grid.minor=element_blank(),legend.position='bottom',axis.text=element_text(color='black'))

# plot of percentages
p1<-
  ggplot(data=binDT,mapping=aes(x=FB_tercile_fac,y=value,group=vax_type_fac))+
  geom_col(mapping=aes(color=vax_type_fac,fill=vax_type_fac),position='dodge')+
  geom_linerange(aes(ymin=lo, ymax=hi),position=position_dodge(width=.9),color='gray80',size=.6)+
  scale_color_manual(values=c('gray60','black'))+
  scale_fill_manual(values=c('gray60','black'))+
  guides(color=F,fill=F)+
  labs(x='Facebook usage',y=NULL,title='Visited at least one page')+
  scale_y_continuous(labels = function(x) paste0(x,"%"))+
  theme_bw()+theme(axis.ticks=element_blank(),panel.border=element_blank(),panel.grid.major.x=element_blank(),plot.title=element_text(hjust=0.5),
                   panel.grid.minor=element_blank(),axis.text=element_text(color='black'),axis.text.x=element_text(margin=margin(-10,0,5,0)))

# plot using count variable
p2<-
  ggplot(data=countDT,mapping=aes(x=FB_tercile_fac,y=value,group=vax_type_fac))+
  geom_col(mapping=aes(color=vax_type_fac,fill=vax_type_fac),position='dodge')+
  geom_linerange(mapping=aes(ymin=lo, ymax=hi),position=position_dodge(width=.9),color='gray80',size=.6)+
  scale_color_manual(values=c('gray60','black'))+
  scale_fill_manual(values=c('gray60','black'))+
  guides(color=F,fill=F)+
  labs(x='Facebook usage',y=NULL,title='Mean number of pages visited')+
  coord_cartesian(ylim=c(0,1.17))+
  scale_y_continuous(breaks=seq(0,1.2,.3))+
  theme_bw()+theme(axis.ticks=element_blank(),panel.border=element_blank(),panel.grid.major.x=element_blank(),plot.title=element_text(hjust=0.5),
                   panel.grid.minor=element_blank(),axis.text=element_text(color='black'),axis.text.x=element_text(margin=margin(-10,0,5,0)))

# arrange plots together
p<-arrangeGrob(arrangeGrob(p1,p2,nrow=1),g_legend(p1.leg),ncol=1,heights=c(1,.08))

# save figure
ggsave('FigureA1.pdf',p,width=8,height=6,units='in',dpi=300)
